// See the Electron documentation for details on how to use preload scripts:
// https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts
const { ipcRenderer, contextBridge } = require('electron');

window.addEventListener('DOMContentLoaded', () => {
  const replaceText = (selector, text) => {
    const element = document.getElementById(selector);
    if (element) element.innerText = text;
  };

  for (const type of ['chrome', 'node', 'electron']) {
    replaceText(`${type}-version`, process.versions[type]);
  }
});

contextBridge.exposeInMainWorld('scratchpad', {
  saveContent: (content) => {
    console.log('preload', content);
    ipcRenderer.send('saveContent', content);
  },
  openFile: () => ipcRenderer.invoke('dialog:openFile'),
  onUpdateCounter: (callback) =>
    ipcRenderer.on('updateCounter', (event, value) => callback(value, value))
});
